<template>
  <view class="wz_black xd tc">
    <view class="sy_bg fz_12" style="height: 200px">
      <view class="wh_9-5 jz" style="padding: 20px 0">
        <!--        配送方式-->
        <view class="split_2 wz_jz wh_max wz_black boxs"
              style="border: 1px solid #3a9e5f;
              border-radius: 20px;background-color: #e6f6eb">
          <view class="fz_14" :class="psfs_id===item.id?'psfs_xz':'psfs'"
                v-for="(item,index) in psfs" :key="index"
                @click="addPsfs(item.id)"
                style="border-radius: 20px;padding: 3px 0">
            {{ item.title }}
          </view>
        </view>
        <!--    地址模块-->
        <view class="ba_white xyy boxs pad_20 mag_t-20" style="border-radius: 10px">
          <view v-if="psfs_id === 3">
            <view v-if="!addressStatus" class="wh_f">
              <view @click="addRess" class="flex_1 fz_20 em">
                <text>请根据统一配送地址</text>
              </view>
              <view class="wz_jz" style="width:5px">
                <image  src="../../static/icon/right.png" style="width: 5px;height: 10px"></image>
              </view>
            </view>
            <view v-if="addressStatus">
              <view>
                <view class="wh_f">
                  <view class="flex_1 fz_16 em">
                    {{ address.address_all }}
                  </view>
                  <view>
                    <image @click="addRess" src="../../static/icon/bj.png"
                           style="width: 16px;height: 16px"/>
                  </view>
                </view>
                <view class="pad_10_0 fz_14">
                  {{ address.address }}
                </view>
                <view>
                  {{ address.name }}
                  <text> {{ address.phone }}</text>
                  <text class="mag_l_5" style="border-radius: 3px;padding: 2px 5px;
        background-color: #d5f0dd;color: #3a9e5f"> 默认地址</text>
                </view>
              </view>
            </view>
            <view class="fz_12 pad_10_0" style="color:#3a9e5f">
              <text>根据你常用地址自动能够选择, 请确认</text>
            </view>
            <view v-if="false" class="wh_f em">
              <view class="flex_1">
                送达时间
              </view>
              <view class="wz_lv ">
                <text>请选择送达时间</text>
                <image class="mag_l-10 " src="../../static/icon/right.png" style="width: 5px;height: 10px"></image>
              </view>
            </view>

            <view v-if="false" class="pad_t-10" style="color:#3a9e5f">因商家繁忙, 送达时间可能波动</view>

          </view>
          <view v-if="psfs_id === 1">

            <view v-if="jwd">
              <view v-if="zdstatus" class="wh_f">
                <view @click="addsite" class="flex_1 fz_20 em">
                  <text>{{ zdxx.name }}</text>
                </view>
                <view class="wz_jz" style="width:5px">
                  <image  src="../../static/icon/right.png" style="width: 5px;height: 10px"></image>
                </view>
              </view>
              <view v-if="!zdstatus" class="wh_f">
                <view @click="addsite" class="flex_1 fz_18 em">
                  <text>点击选择站点</text>
                </view>
                <view class="wz_jz" style="width:5px">
                  <image  src="../../static/icon/right.png" style="width: 5px;height: 10px"></image>
                </view>
              </view>
            </view>
            <view v-if="!jwd">
              <view class="wh_f">
                <view @click="getAddress" class="flex_1 fz_20 em wh_f_r">
                  <view class="czjz_flex"
                        style="margin-right: 10px">
                    <up-icon name="map"></up-icon></view>
                  <view class=" fz_14">
                    <text>点击以获取定位</text>
                  </view>
                </view>
              </view>
            </view>
          </view>
        </view>
        <!--    商品模块-->
        <view class="ba_white xyy boxs mag_t-20" style="border-radius: 10px;padding: 10px 20px">
            <view
                  v-for="(item,index) in list"
                  :key="index">
              <view class="wh_f fz_14 mag_t-10">
                <view style="width: 60px">
                  <view class="img_zfx">
                    <image :src="item.goods.image[0]"></image>
                  </view>
                </view>
                <view class="flex_1 boxs wz_hui " style="padding-left: 10px">
                  <view class="fz_16 em wz_black">{{ item.goods.name }}</view>
                  <view>{{ item.goods.specs }}</view>
                  <view>x {{ item.amount }}</view>
                </view>
                <view style="width: 60px">
                  <view class="em fz_16">￥{{ item.goods.price }}</view>
                  <view class="scx wz_hui">￥{{ item.goods.re_price }}</view>
                </view>
              </view>
            </view>
          <view v-if="false" @click="zk" class="wz_jz wz_hui">
            <text>展开 (共{{ list.length}}件)</text>
          </view>
        </view>
        <!--    活动模块-->
        <view class="ba_white xyy boxs mag_t-20 fz_14 spjs" style="border-radius: 10px;padding: 20px 20px 10px">
          <view class="wh_f">
            <view class="em flex_1 wz_black">商品总价</view>
            <view>
              <text v-if="yhqTrue" class="scx wz_hui">￥{{ money.coupon_before_price }}</text>
              <text class="em mag_l-5">￥{{ money.total_price }}</text>
            </view>
          </view>
          <view v-if="false" class="wh_f">
            <view class="em flex_1 wz_black">活动</view>
            <view>
              <text v-if="hd.name">{{hd.name}}</text>
              <text v-if="!hd.name" class="scx wz_hui">暂无活动</text>
            </view>
          </view>
          <view v-if="false" class="wh_f">
            <view class="em flex_1 wz_black">
              云享{{ money.level }}级
              <text class="bor_5 mag_l-10 fz_10"
                    style="background-color: #fef5df;padding:0 5px 0;color: #ff9a2e;border: 1px solid #ff9a2e">
                <text>{{ money.discount }}折</text>
              </text>
            </view>
            <view>
              <text class="scx wz_hui">等级折扣价格</text>
              <text class="em mag_l-5">￥{{ money.level_discount_price }}</text>
            </view>
          </view>
          <view v-if="yhq" class="wh_f">
            <view class="em flex_1 wz_black">优惠券</view>
            <view>
              <view @click="tcYhq" v-if="yhq?.list?.length>0 && yhq?.yes_use_num > 0" class="wz_hui">
                <text v-if="!yhqTrue" class="centerdq">
                  共{{ yhq?.list?.length }}张 , {{ yhq?.yes_use_num }}张可用
                </text>
                <text class="centerdq" v-if="yhqTrue">{{yhqItem[0].name}}</text>
                <img style="width: 6px;height: 12px" class="centerdq mag_l-5" src="../../static/icon/right.png" alt="" />
              </view>
              <text class="wz_hui" v-if="yhq?.list?.length===0 || yhq?.yes_use_num==0">
                暂无可用优惠券
              </text>
            </view>
          </view>
          <view class="wh_f"
                style="border-bottom: 1px solid #edebeb">
            <view class="em flex_1 wz_black">配送费</view>
            <view>
              <text class="wz_qred" v-if="money.need_delivery_fee == 0">配送费活动-{{ money.init_delivery_fee }}元</text>
              <text class="scx wz_hui mag_l-5" v-if="money.need_delivery_fee == 0">￥{{ money.init_delivery_fee }}</text>
              <text class="em mag_l-5">￥{{ money.need_delivery_fee }}</text>
            </view>
          </view>
          <view class="wz_rigdq fz_14" style="padding: 10px 0 0">
            <text class="wz_red mag_r-10 bottomdq">已优惠 ¥ {{money.discount_price }}</text>
            <text class=" bottomdq">小计:</text>
            <text class="fz_10 mag_r-5 bottomdq"> ¥</text>
            <text class="em bottomdq">{{money.pay_price }}</text>
          </view>
        </view>
<!--        运费抵扣券-->
        <view v-if="yflist.length>0" class="ba_white xyy boxs mag_t-20 fz_14 spjs" style="border-radius: 10px;padding: 0 20px 5px">
          <view class="pad_b_10 wz_hei em">运费抵扣券</view>
          <radio-group>
            <view v-for="(item,index) in yflist" :key="index"
                  class="wh_f_l pad_l-10 boxs wh_max" style="margin-bottom: 10px">
              <view class="">
                <view class="wh_f_l wh_max">
                  <view class="ccsl_1">
                    <image class="centerdq" src="../../static/icon/yhq.png" style="width: 20px;height: 20px" />
                    <text class="centerdq mag_l-10 fz_14">{{ item.type_str }}</text>
                    <text class="centerdq mag_l-5 fz_14">{{ item.value }}元</text>
                  </view>
                </view>
                <view>
                </view>
              </view>
              <view>
                <radio @click="addyf(item)" :value="item.id" />
              </view>
            </view>
          </radio-group>
        </view>

        <!--        随手购-->
        <view v-if="Reallist.length>0" class="ba_white xyy boxs mag_t-20 fz_14 spjs" style="border-radius: 10px;padding: 0 20px 5px">
          <view class="pad_b_10 wz_hei em">福袋</view>
          <radio-group>
            <view v-for="(item,index) in Reallist" :key="index"
                  class="wh_f_l pad_l-10 boxs wh_max" style="margin-bottom: 10px">
              <view class="">
                <view class="wh_f_l wh_max">
                  <view class="ccsl_1">
                    <image class="centerdq" src="../../pagesList/img/libao.png" style="width: 20px;height: 20px" />
                    <text class="centerdq mag_l-10 fz_14">{{ item.name }}</text>
                  </view>
                </view>
                <view></view>
              </view>
              <view>
                <radio @click="addfd(item)" :value="item.id" />
              </view>
            </view>
          </radio-group>
        </view>
        <!--        支付模块-->
        <view class="ba_white xyy boxs mag_t-20 fz_14 spjs" style="border-radius: 10px;padding: 10px 20px">
          <view class="wh_f"  @click="pay_id = 1">
            <view class="flex_1">
              <image class="centerdq" src="../../static/icon/ye.png" style="width: 30px;height: 30px"/>
              <text class="wz_black centerdq mag_l-5 em">余额支付</text>
            </view>
            <view class=" czjz_flex">
              <text class="centerdq ">余额: ¥ {{ userInfo.cash_balance }}
              </text>
              <image v-if="pay_id===2" class="mag_l-10" src="../../static/icon/zfk.png" style="width: 20px;height: 20px"/>
              <image v-if="pay_id===1" class="mag_l-10" src="../../static/icon/chenggong.png" style="width: 20px;height: 20px"/>
            </view>
          </view>
          <view class="wh_f" @click="pay_id = 2">
            <view class="flex_1">
              <image class="centerdq" src="../../static/icon/wxzf.png" style="width: 30px;height: 30px"/>
              <text class="wz_black centerdq mag_l-5 em">微信支付</text>
            </view>
            <view class=" czjz_flex">
              <image v-if="pay_id===1" class="mag_l-5" src="../../static/icon/zfk.png" style="width: 20px;height: 20px"/>
              <image v-if="pay_id===2" class="mag_l-5" src="../../static/icon/chenggong.png" style="width: 20px;height: 20px"/>
            </view>
          </view>
        </view>
      </view>
      <view style="height:60px;"></view>
    </view>
    <!--    底部菜单-->
    <view class="pm ba_white box_yy" style="width: 100%;height:60px;left: 0;bottom: 0">
      <view class="wh_f he_max">
        <view class="flex_1 wh_f czjz_flex ">
          <view class="boxs pad_l-20">
            <view>
              <text class="wz_hui">待支付 </text>
              <text class="wz_qred fz_10"> ¥ </text>
              <text class="wz_qred em fz_18"> {{ money.pay_price }}</text>
            </view>
            <view class="wz_qred fz_10">已优惠 ¥{{ money.discount_price }}</view>
          </view>
        </view>
        <view class="czjz_flex he_max">
          <view @click="isPayOrder" class="jz_flex wz_white bor_25 mag_r-10"
                style="background-color: #e63946;width: 110px;height: 40px">
            <text>去支付</text>
          </view>
        </view>
      </view>
    </view>
    <!--    确定弹窗-->
    <up-popup :safeAreaInsetBottom="false"
              @close="close"
              @open="open"
              customStyle="width:70%;border-radius: 20px"
              :show="isYEstatus" mode="center">
      <view>
        <view style="padding: 15px">
          <view class="fz_16 wz_jz em pad_b_10">
            <text>确认付款</text>
          </view>
          <view class="split_2 pad_10_0 wz_jz">
            <view>
              <text class="bor_1_hui bor_20 fz_12 em" @click="isYEstatus=false"
                    style="background-color: #d5f0dd;color: #3a9e5f;
                  padding: 5px 30px 6px">取 消</text>
            </view>
            <view>
              <text class="bor_1_hui bor_20 fz_12 em"
                    @click="addOrder"
                    style="background-color: #3a9e5f;color:white;
                  padding: 5px 30px 6px">确 定</text>
            </view>
          </view>
        </view>
      </view>
    </up-popup>
    <up-popup :show="isAddressShow" mode="bottom"
              @close="isAddressShow=false"
              @open="isAddressShow=true">
      <view>
        <view class="fz_18 em pad_20">请选择站点</view>
        <view>
          <view class="wh_f">
            <view class="flex_1 wh_max">
              <view class="box2 margin-top-sm fz_16  ">
                <radio-group @change="radioChange">
                  <view v-for="(item,index) in siteList" :key="index"
                        class="wh_f_l pad_l-20 boxs wh_max" style="height: 50px;">
                    <view class="">
                      <view class="wh_f_l wh_max">
                        <view class="ccsl_1">
                          <image class="centerdq" src="../../static/icon/yz.png" style="width: 24px;height: 24px" />
                          <text class="centerdq mag_l-10 fz_16">{{ item.name }}</text>
                        </view>
                        <view>
                          <text class="bottomdq mag_l-10 fz_12 pad_0_10 boxs">
                            距您 {{item.distant}} km
                          </text>
                        </view>
                      </view>
                    </view>
                    <view>
                      <radio @click="addzd(item)" :value="item.id" :checked="pay_type===item.id" />
                    </view>
                  </view>
                </radio-group>
              </view>
            </view>
          </view>
        </view>
        <view class="wz_jz wz_lv fz_12 pad_20_0">
          <text @click="gdyz">更多驿站</text>
        </view>
      </view>
    </up-popup>
    <up-popup :show="isyhq" mode="center" >
      <view class="boxs" style="padding: 20px 15px 0">
        <view class="fz_18 em pad_b_20 wh_f_l">
          <view>请选择优惠券</view>
          <view>
            <up-icon @click="Yhqok"
                size="20"
                name="close"
            ></up-icon>
          </view>
        </view>
        <view class="boxs" style="height: 50vh;">
          <scroll-view :scroll-y="true" style="height: 100%;width: 100%">
            <view class="pad_5 boxs">
              <view v-for="(item,index) in yhq?.list" class="bor_10 ba_white xyy mag_b_10">
                <view v-if="item.is_use == 0" @click="addYhq(item)">
                  <view class="wh_f pad_15 boxs">
                    <view class="flex_3">
                      <view class="fz_16 wz_black">{{item.name}}</view>
                      <view class="fz_12 wz_hui pad_10_0">有效期至 {{item.end_date}}</view>
                      <view class="fz_12 wz_hui">来源 {{item.cate_str}}</view>
                    </view>
                    <view class="flex_1 wz_jz wz_red">
                      <view>
                        ¥
                        <text class=" fz_24">{{item.value}}</text>
                        <text> 元</text>
                      </view>
                    </view>
                    <view class="jz_flex" style="width: 40px">
                      <view>
                        <image
                            v-if="yhq_id !== item.id"
                            src="../../static/icon/zfk.png"
                            style="width: 20px;height: 20px">
                        </image>
                        <image
                            v-if="yhq_id === item.id"
                            src="../../static/icon/chenggong.png"
                            style="width: 20px;height: 20px">
                        </image>
                      </view>
                    </view>
                  </view>
                </view>
              </view>
            </view>
          </scroll-view>
        </view>
      </view>
      <view class="wz_rigdq" style="padding: 15px 0;border-top: 1px solid #e7e7e7">
        <text @click="Yhqok" class="mag_r-20" style="
          border-radius: 20px;
          padding:8px 30px;
          color: #3a9e5f;
          background-color: #d5f0dd">确 定</text>
      </view>
    </up-popup>
    <up-toast ref="uToastRef"></up-toast>
  </view>
</template>
<script>
//在要使用服务的页面
import {userCenter} from "../../Api/user";

var QQMapWX = '@/utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.js';
let qqmapsdk;
import {
  chooseCoupon,
  createYzOrder,
  getAct,
  getShopStoreList,
  miniPay, orderDetail, rewardLogs,
  settlementDetail,
  shoppingCartList
} from "../../Api/shop";

export default {
  name: "index",
  data() {
    return {
      jwd:false,
      lengthIndex:false,
      pay_type:1, //站点id
      zd_id:1,  //站点id
      addressStatus:false,
      isAddressShow: false, //站点列表开关
      psfs: [
        {id: 1, title: "驿站自提"},
        {id: 3, title: "配送到家"}
      ],
      psfs_id: 1,
      shopList: [
        {
          id: 1,
          imgUrl: '../../static/img/nl.png',
          name: '泥螺',
          ys: '1000+',
          kc: 1998,
          money: '69.88',
          text: '收藏100+',
          age: 330,
          yj: "79.00",
          sum: 0
        },
        {
          id: 2,
          imgUrl: '../../static/img/hz.png',
          ys: '900+',
          kc: 2003,
          name: '海蜇',
          money: '147.88',
          text: '10+回头客',
          age: 500,
          yj: "169.00",
          sum: 0
        },
        {
          id: 3,
          imgUrl: '../../static/img/kj.png',
          ys: '700+',
          kc: 1000,
          name: 'Kj大骨头',
          money: '12.64',
          text: '收藏100+',
          age: 250,
          yj: "28.00",
          sum: 0
        },
      ],
      pay_id: 1,
      form:{
        isDefault:false
      },
      isShow:false,
      ssqList:{},
      address:{},
      list:[],
      hd:[],
      money:{},
      yhq:[],
      siteList:[], //站点列表
      zdstatus:false, //默认不选站点
      zdxx:{},
      order:null, //订单id
      ageList:{}, //价格单
      isyhq:false,
      reward_logs_id:0,
      yhqItem:[],
      yhqTrue:false,
      userInfo:{},
      yhq_id:-1,
      item:{},
      store_id:0,
      isYEstatus:false,
      isTrue:false,
      act_id:null,
      goods_ids:[],
      Reallist:[],
      yflist:[],
      fd_id:0,
      yf_id:0
    }
  },
  methods: {
    zk(){
      this.lengthIndex = true
    },
    gdyz(){
      uni.navigateTo({
        url: '/pagesList/dakList/index'
      });
    },
    // 点击了优惠券
    addYhq(e){
      if(this.yhq_id === e.id){
        console.log("取消选择优惠券")
        this.yhq_id = 0
        this.reward_logs_id = 0
        this.item = {}
      }else{
        console.log("选择了优惠券",e.id)
        this.reward_logs_id = e.id
        this.yhq_id = e.id
        this.item = e
      }
    },
    // 确定选择优惠券
    Yhqok(){
      if(this.item.id){
        if(this.yhqItem.length<1) {
          this.yhqItem = [this.item]
          this.yhqTrue = true
          this.reward_logs_id = this.yhq_id
        }
      }else{
        this.yhqItem = []
        this.yhqTrue = false
        this.reward_logs_id = 0
      }
      this.isyhq = false
      this.countShop(this.list)
    },
    addYhq_old(e){
      if(this.yhqItem.length<1){
        this.yhqItem = [e]
        this.yhqTrue = true
        this.reward_logs_id = e.id
        this.countShop(this.list)
      }else{
        this.yhqTrue = false
        this.yhqItem = []
        this.reward_logs_id = 0
        this.countShop(this.list)
      }
      this.isyhq = false
    },
    // 计算价格
    countShop(e){
      //如果是驿站模式
      // if(this.psfs_id == 3){
      //   this.store_id = item.id
      //   this.countShop(this.list)
      // }
      console.log("优惠券id",this.reward_logs_id)
      settlementDetail({
        reward_logs_id:this.reward_logs_id,
        data:e,
        act_id:this.act_id,
        fd_id:this.fd_id,
        store_id:this.psfs_id === 3?0:this.store_id,
        yf_id:this.yf_id
      }).then(res=>{
        this.money = res.data
        chooseCoupon({
          price:res.data.coupon_before_price,
          goods_ids:this.goods_ids.slice(0, -1)
        }).then(res=>{
          // let data;
          // if(this.isTrue){
          //   data = res.data.list.filter(item=>{
          //     if(item['value'] == 5){
          //       return item
          //     }
          //   })
          //   this.yhq.list = data
          //   this.yhq.yes_use_num = data.length
          // }else {
          //   this.yhq = res.data
          // }
          this.yhq.list = res.data.list
          this.yhq.yes_use_num = res.data.list.length
          console.log('优惠券',this.yhq.list)
        })
      })
    },
    getList(e){
      this.list = e
    },
    // 点击福袋
    addfd(e){
      this.fd_id = e.id
    },
    // 点击运费券
    addyf(e){
      this.yf_id = e.id
      this.countShop(this.list)
    },
    // 点击站点
    addzd(e){
      console.log(e)
      this.zdxx = e
      uni.setStorageSync('dak',e);
      //查询站点配送费
      this.store_id = e.id
      this.countShop(this.list)

      this.isAddressShow = false
      this.zdstatus = true
    },
    // 打开站点列表
    addsite(){
      this.isAddressShow = true
    },
    //站点列表
    getsiteList(){
      let jwd = uni.getStorageSync('latAddress');
      let add = JSON.parse(jwd)
      getShopStoreList({
        page:1,
        per_page:5,
        longitude:add.longitude,
        latitude:add.latitude
      }).then(res=>{
        console.log('站点列表',res.data)
        this.siteList = res.data.data

        let item = uni.getStorageSync('dak');
        if(item){
          this.zdxx = item
          //如果是驿站模式
          if(this.psfs_id !== 3){
            console.log("我是驿站",item)
            this.store_id = item.id
            this.countShop(this.list)
          }
          this.jwd = true
          this.zdstatus = true
        }
      })
    },
    radioChange(e){
      this.pay_type = e
    },
    // 下单
    addOrder(){
      //余额支付
      if(this.pay_id === 1){
        switch (this.psfs_id){
          case 1:
            if(this.zdxx.id){
              createYzOrder({
                reward_logs_id:this.reward_logs_id, //活动id
                get_type:this.psfs_id, //送货 自提
                data:this.list, //商品列表
                origin:1,
                act_id:this.act_id,
                yf_id:this.yf_id,
                fd_id:this.fd_id,
                delivery_type:1,
                pay_type: "3", //余额支付
                stage_store_id:this.zdxx.id //站点id
              }).then(res=>{
                if(res.success){
                  const params = { id:res.data.order_id}; // 待传递的参数
                  uni.navigateTo({
                    url: `/porder/orderDetail/index?id=${params.id}`
                  });
                }
                console.log('支付结果',res)
              })
            }else{
              this.showToast('请选择自提站点!')
            }
            break
          case 3:
            if(this.address.id){
              createYzOrder({
                reward_logs_id:this.reward_logs_id, //活动id
                get_type:3, //送货
                data:this.list, //商品列表
                origin:1,
                act_id:this.act_id,
                fd_id:this.fd_id,
                yf_id:this.yf_id,
                pay_type: "3", //余额支付
                address_id:this.address.id //地址id
              }).then(res=>{
                if(res.success){
                  const params = { id:res.data.order_id}; // 待传递的参数
                  uni.navigateTo({
                    url: `/porder/orderDetail/index?id=${params.id}`
                  });
                }
                console.log('支付结果',res)
              })
            }else{
              this.showToast('请选择收货地址!')
            }
            break
        }
      }else{
        // 微信支付
        switch (this.psfs_id){
          case 1:
            if(this.zdxx.id){
              createYzOrder({
                reward_logs_id:this.reward_logs_id, //活动id
                get_type:this.psfs_id, //送货 自提
                data:this.list, //商品列表
                origin:1,
                act_id:this.act_id,
                fd_id:this.fd_id,
                yf_id:this.yf_id,
                pay_type: "1", //微信支付
                stage_store_id:this.zdxx.id //站点id
              }).then(res=>{
                if(res.success){
                  this.order = res.data.order_id
                  if(parseInt(this.money.pay_price) === 0){
                    const params = { id:this.order}; // 待传递的参数
                    uni.navigateTo({
                      url: `/porder/orderDetail/index?id=${params.id}`
                    });
                  }else{
                    this.buy(res.data.order_id)
                  }
                }
              })
            }else{
              this.showToast('请选择自提站点!')
            }
            break
          case 3:
            if(this.address.id){
              createYzOrder({
                reward_logs_id:this.reward_logs_id, //活动id
                get_type:3, //送货
                data:this.list, //商品列表
                origin:1,
                act_id:this.act_id,
                fd_id:this.fd_id,
                pay_type: "1", //微信支付
                address_id:this.address.id, //地址id
                yf_id:this.yf_id
              }).then(res=>{
                if(res.success){
                  this.order = res.data.order_id
                  console.log('支付金额',this.money.pay_price)
                  if(this.money.pay_price == 0){
                    const params = { id:this.order}; // 待传递的参数
                    uni.navigateTo({
                      url: `/porder/orderDetail/index?id=${params.id}`
                    });
                  }else{
                    this.buy(this.order)
                  }
                }
              })
            }else{
              this.showToast('请选择收货地址!')
            }
            break
        }
      }
    },
    // 下单
    isPayOrder(){

      console.log("团购商品2",this.act_id)
      let act_id = this.act_id
      //余额支付
      if(this.pay_id === 1){
        this.isYEstatus = true
      }else{
        // 微信支付
        switch (this.psfs_id){
          case 1:
            if(this.zdxx.id){
              createYzOrder({
                reward_logs_id:this.reward_logs_id, //活动id
                get_type:this.psfs_id, //送货 自提
                data:this.list, //商品列表
                origin:1,
                fd_id:this.fd_id,
                act_id:this.act_id,
                pay_type: "1", //微信支付
                stage_store_id:this.zdxx.id, //站点id
                yf_id:this.yf_id
              }).then(res=>{
                if(res.success){
                  this.order = res.data.order_id
                  if(parseInt(this.money.pay_price) === 0){
                    const params = { id:this.order}; // 待传递的参数
                    uni.navigateTo({
                      url: `/porder/orderDetail/index?id=${params.id}`
                    });
                  }else{
                    this.buy(res.data.order_id)
                  }
                }
              })
            }else{
              this.showToast('请选择自提站点!')
            }
            break
          case 3:
            if(this.address.id){
              createYzOrder({
                reward_logs_id:this.reward_logs_id, //活动id
                get_type:3, //送货
                data:this.list, //商品列表
                origin:1,
                fd_id:this.fd_id,
                act_id:this.act_id,
                pay_type: "1", //微信支付
                address_id:this.address.id, //地址id,
                yf_id:this.yf_id
              }).then(res=>{
                if(res.success){
                  this.order = res.data.order_id
                  console.log('支付金额',this.money.pay_price)
                  if(this.money.pay_price == 0){
                    const params = { id:this.order}; // 待传递的参数
                    uni.navigateTo({
                      url: `/porder/orderDetail/index?id=${params.id}`
                    });
                  }else{
                    this.buy(this.order)
                  }
                }
              })
            }else{
              this.showToast('请选择收货地址!')
            }
            break
        }
      }
    },
    // 微信支付
    buy(order_id) {
      let tthis = this
      miniPay({
        order_id: order_id,
        type: 2
      }).then(res => {
        let data = res.data
        console.log(res)
        uni.requestPayment({
          provider: 'wxpay',
          timeStamp: data.timeStamp,
          nonceStr: data.nonceStr,
          package: data.package,
          signType: data.signType,
          paySign: data.paySign,
          _debug: 1,
          success: (res) => {
            tthis.showToast('支付成功!')
            const params = { id:order_id}; // 待传递的参数
            uni.navigateTo({
              url: `/porder/orderDetail/index?id=${params.id}`
            });
            console.log('支付结果',res)

            // this.$refs.uToast.show({
            //   message: '支付成功',
            //   type: 'success',
            //   complete() {
            //     uni.redirectTo({
            //       url: '../skittles/My_order'
            //     })
            //   }
            // })
          },
          fail: (error) => {
            uni.showToast({
              icon: 'none',
              title: '支付失败'
            })
            if(this.order){
              const params = { id:this.order}; // 待传递的参数
              uni.navigateTo({
                url: `/porder/orderDetail/index?id=${params.id}`
              });
            }

          }
        })
      })
    },
    //消息提示
    showToast(msg, type = "default") {
      uni.showToast({
        title: msg,
        icon: "none", // 图标类型，可选值：success, none
        duration: 2000 // 显示时间，单位毫秒
      });
    },
    getMoney(){
      getAct().then(res=>{
        this.hd = res.data
      })
      settlementDetail({
        reward_logs_id:this.reward_logs_id,
        data:this.list
      }).then(res=>{
        this.money = res.data
        chooseCoupon({
          price:res.data.total_price
        }).then(res=>{
          this.yhq = res.data
          console.log('优惠券',res)
        })
        console.log('价格详情',res.data)
      })

    },
    updateAddress(){
      uni.navigateTo({
        url: '/porder/userAddress/index'
      });
    },
    addRess(){
      uni.navigateTo({
        url: '/pagesList/userAddress/index'
      });
    },
    addPsfs(e) {
        this.psfs_id = e
        if(e === 3){
          this.store_id = 0
          this.countShop(this.list)
        }else {
          //如果有缓存站点 则直接去查询站点配送费价格
          let item = uni.getStorageSync('dak');
          if(item){
            this.store_id = item.id
            this.countShop(this.list)
          }else{

          }
        }
    },
    getAddress(){
      var that = this
      wx.authorize({
        scope: 'scope.userLocation',//发起定位授权
        success: function () {
          wx.getLocation({
            type: 'wgs84',
            success(res) {
              const latitude = res.latitude
              const longitude = res.longitude
              const speed = res.speed
              const accuracy = res.accuracy
              let latAddress = JSON.stringify({
                latitude : res.latitude,
                longitude : res.longitude,
                speed : res.speed,
                accuracy : res.accuracy
              })
              console.log(latAddress)

              uni.setStorageSync('latAddress',latAddress);
              that.getsiteList()
              that.jwd =  true

            }
          })
          //授权成功，此处调用获取定位函数
        }, fail() {
          this.psfs_id = 3
          //如果用户拒绝授权，则要告诉用户不授权就不能使用，引导用户前往设置页面。
          console.log('没有定位授权')
          wx.showModal({
            cancelColor: 'cancelColor',
            title: '没有授权无法获取位置信息',
            content: '是否前往设置页面手动开启',
            success: function (res) {
              if (res.confirm) {
                wx.openSetting({
                  withSubscriptions: true,
                })
              } else {
                wx.showToast({
                  icon: 'none',
                  title: '您取消了定位授权',
                })
              }
            }, fail: function (e) {
              console.log(e)
            }
          })
        }
      })

    },
    change(){

    },
    tcYhq(){
      if(this.yhq?.yes_use_num>0){
        this.isyhq = true
      }
    },
    // 获取实物券
    getRealList(){
      rewardLogs({
        type:3,
        is_use:0
      }).then(res=>{
        this.Reallist = res.data
      })
    },
    // 获取运费抵扣券
    getyfList(){
      rewardLogs({
        type:5,
        is_use:0
      }).then(res=>{
        if(res.data.length>0){
          this.yflist = [res.data[0]]
          console.log('运费1',res.data)
          console.log('运费2',this.yflist)
        }
      })
    }
  },
  onLoad(options){
    let list = JSON.parse(decodeURIComponent(options.list))
    console.log("穿透",list)
    list.forEach(item => {
      this.goods_ids += item.goods.id + 'A'

      // if(item.goods.category_pstr == '礼品卡'){
      //   this.isTrue = true
      // }
    })

    this.list = list
    if(list[0].act_id){
      this.act_id = list[0].act_id
      console.log("团购商品",this.act_id)
    }else{
      console.log("普通商品")
    }
    console.log(222,list)
    // settlementDetail({
    //   reward_logs_id:this.reward_logs_id,
    //   data:list,
    //   store_id:this.psfs_id === 3?0:this.store_id
    // }).then(res=>{
    //   this.money = res.data
    //   chooseCoupon({
    //     price:res.data.coupon_before_price
    //   }).then(res=>{
    //     this.yhq = res.data
    //     console.log('优惠券',res)
    //   })
    //   console.log('商品价格',res)
    // })

  },
  onShow(){
    userCenter().then(res=>{
      this.userInfo = res.data
    })

    this.getRealList()
    this.getyfList()

    uni.$on('yzid',(data)=>{
      this.addzd(data)
    })

    // qqmapsdk = new QQMapWX({
    //   key: 'K6RBZ-GSGET-UMZXN-VQ2KS-24ZZT-54FJO'
    // });
    let jwd =  uni.getStorageSync('latAddress');

    if(jwd){
      this.jwd = true
      this.getsiteList()
    }else{
      this.jwd = false
      this.countShop(this.list)
    }
  },
  mounted() {
    uni.$on('ref',(data)=>{
      this.address = data.data
      this.addressStatus = true
      console.log("传递了",data)
    })
  }
}
</script>
<style scoped lang="scss">


::v-deep.u-popup__content{
  border-radius: 10px;
}
.mx_2 {
  width: 690upx;
  height: 60upx;
  padding-left: 30upx;
  padding-right: 20upx;
  font-size: 32upx;
  margin-top: 5upx;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .ticket {
    color: #7E7C7D;
    font-family: PingFang SC;
    font-size: 26upx;
    font-style: normal;
    font-weight: 400;
    display: flex;
    align-items: center;
  }
}

.dxk {
  ::v-deep.data-v-6fd02e59 {
    width: 100%;
  }
}
  .spjs > view {
    padding: 10px 0;
  }
  .tc {
    ::v-deep .u-popup__content {
      width: 90%;
    }
  }
  .sy_bg {
    background-image: url("https://xdjzapi.towngo.cn/wx/indextopbj.png");
    background-size: 100% 100%;
  }

  .psfs_xz {
    background-color: #3a9e5f;
    color: white;
  }

  .psfs {
    color: #3a9e5f;
  }

  .xz_lx {
    height: 30px;
    width: 6px;
    background-color: #3a9e5f;
    border-radius: 0 10px 10px 0;
  }

  .xz_menu {
    background-color: white;
  }
</style>
